package com.southminority.ethnic.controller.research;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.research.ResearchProjectService;
import com.southminority.ethnic.controller.research.vo.ResearchProjectVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

/**
 * 科研项目前端展示控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@RestController
@RequestMapping("/api/research/project")
@Tag(name = "18 - 科研项目展示API", description = "【前台】科研成果-科研项目查询与展示相关接口")
@CrossOrigin
public class ResearchController {

    private final ResearchProjectService researchProjectService;

    public ResearchController(ResearchProjectService researchProjectService) {
        this.researchProjectService = researchProjectService;
    }

    @GetMapping("/page")
    @Operation(summary = "分页和条件查询已发布的科研项目列表", description = "用于前端科研项目列表页面，支持按项目类型、级别和开始年份筛选。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "查询成功")})
    public R<PageResult<ResearchProjectVO>> getPublishedProjectPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页显示数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "项目类型", schema = @Schema(type = "string", allowableValues = {"ongoing", "completed"})) @RequestParam(required = false) String projectType,
            @Parameter(description = "项目级别", example = "国家级") @RequestParam(required = false) String level,
            @Parameter(description = "开始年份", example = "2023") @RequestParam(required = false) Integer year) {
        return R.ok(researchProjectService.getPublishedProjectPage(page, size, projectType, level, year));
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单个科研项目的详细信息", description = "根据项目ID查询，返回其完整的公开信息。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "项目不存在或未发布")
    })
    public R<ResearchProjectVO> getResearchProjectDetail(
            @Parameter(description = "科研项目ID", example = "1", required = true) @PathVariable Long id) {
        return R.ok(researchProjectService.getProjectDetail(id));
    }

    @GetMapping("/levels")
    @Operation(summary = "获取所有项目级别列表", description = "用于前端筛选菜单，返回一个包含所有不重复项目级别的字符串列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<String>> getProjectLevels() {
        return R.ok(researchProjectService.getProjectLevels());
    }

    @GetMapping("/start-years")
    @Operation(summary = "获取所有项目开始年份列表", description = "用于前端筛选菜单，返回一个包含所有不重复开始年份的列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<Integer>> getProjectStartYears() {
        return R.ok(researchProjectService.getProjectStartYears());
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性")
    public R<String> test() {
        return R.ok("科研项目前端服务正常");
    }
} 
